package com.jpyl.jpMedicalTreatment.view.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.forxindu.kit.box.ViewKit;
import com.jpyl.jpMedicalTreatment.R;
import com.jpyl.jpMedicalTreatment.common.MedicalSettings;
import com.jpyl.jpMedicalTreatment.model.ResponseModel;
import com.jpyl.jpMedicalTreatment.network.Network;
import com.jpyl.jpMedicalTreatment.network.api.ApiError;
import com.jpyl.jpMedicalTreatment.network.api.RequestApi;

import butterknife.BindView;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by SYL on 2017/4/27.
 */

public class ResetPasswordActivity extends BaseActivity{

    @BindView(R.id.password_number)
    EditText password;

    @BindView(R.id.password_number_again)
    EditText passwordAgain;

    @BindView(R.id.btn_updatePwd)
    EditText updatePwdBtn;

    private String phoneNumber;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_reset_password;
    }

    @Override
    public void afterCreate(Bundle savedInstanceState) {
        mTitleTv.setText("修改密码");
        phoneNumber = getIntent().getStringExtra(MedicalSettings.MEMBER_MOBILE);
        updatePwdBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String pwd = password.getText().toString().trim();
                final String pwdAgain = passwordAgain.getText().toString().trim();
                if (!TextUtils.isEmpty(pwd) && !TextUtils.isEmpty(pwdAgain)) {
                    if ( pwd.length() >= 1 && pwdAgain.length() >= 1  && pwdAgain.equals(pwd)) {
                        RestorePassword(phoneNumber);
                    }
                }
            }
        });
        passwordAgain.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                setRegisterButtonStatus();
            }
        });

        password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                setRegisterButtonStatus();
            }
        });
    }

    private void setRegisterButtonStatus() {

        final String pwd = password.getText().toString().trim();
        final String pwdAgain = passwordAgain.getText().toString().trim();
        if (!TextUtils.isEmpty(pwd) && !TextUtils.isEmpty(pwdAgain)) {
            if ( pwd.length() >= 1 && pwdAgain.length() >= 1  && pwdAgain.equals(pwd)) {
                updatePwdBtn.setBackgroundResource(R.drawable.round_corner_blue_bg_shape10);
            } else {
                updatePwdBtn.setBackgroundResource(R.drawable.btn_feed_back_shape);
            }
        }
    }
    final RequestApi publicApi = Network.getRequestApi();
    //用户名用来初始化密码，需在移动端经过验证码验证
    void RestorePassword(String loginname) {

        publicApi.jpylRestorePassword(loginname).map(new Func1<ResponseModel<String>,
                String>() {
            @Override
            public String call(ResponseModel<String> responseModel) {
                if (responseModel.getCode() != 0) {
                    String msg = responseModel.getMsg();
                    throw new ApiError(responseModel.getCode(), msg);
                }
                return responseModel.getMsg();
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (e instanceof ApiError) {
                            ApiError error = (ApiError) e;
                            ViewKit.showToast(ResetPasswordActivity.this, error.getMsg() + "");
                        }
                    }

                    @Override
                    public void onNext(String msg) {
                        ViewKit.showToast(ResetPasswordActivity.this, msg + "");
                    }
                });
    }
}